<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">修改用户组</h4>
        </div>
        <form style="margin:4%;" id="editRoleForm" data-toggle="validator" role="form">
            <div class="form-group">
                <label for="name" class="control-label">用户组:</label>
                <input type="text" id="name" class="form-control" name="name" value="{$role.name}" required>
            </div>
            <div class="form-group">
                <label for="introduce" class="control-label">描述:</label>
                <input type="text" id="introduce" class="form-control" name="introduce"  value="{$role.introduce}" required>
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态:</label>
                <input type="checkbox" name="_status" value="1" value="{$role.status}" />
                <input type="hidden" name="status" value="{$role.status}" />
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">APP登陆:</label>
                <input type="checkbox" name="_app_login" value="0" value="{$role.app_login}" />
                <input type="hidden" name="app_login" value="0" value="{$role.app_login}" />
            </div>
            <div class="form-group">
                <label for="message-text" class="control-label">后台登陆:</label>
                <input type="checkbox" name="_back_login" value="1" value="{$role.back_login}" />
                <input type="hidden" name="back_login" value="1" value="{$role.back_login}" />
            </div>
            <input type="hidden" name="roleId" value="{$role.id}" />
        </form>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
            <button type="button" id="roleSave" class="btn btn-primary">保存</button>
        </div>
    </div>
</div>

<script>
    var status = "{$role.status}";
    var app_login = "{$role.app_login}";
    var back_login = "{$role.back_login}";
        $(function () {
            $('#editRoleForm').validator();
            var switchOption = {
                onText: "开",
                offText: "关",
                onColor: "success",
                offColor: "info",
                state: true,
                onSwitchChange: function (event, state) {
                    if (state == true) {
                        $(this).val("1");
                    } else {
                        $(this).val("0");
                    }
                    var value = $(this).val();
                    $("input:hidden[name='"+$(this).attr("name").substring(1)+"']").val(value);
                }
            };
            if(status == "0"){
                switchOption.state = false;
            }else{
                switchOption.state = true;
            }
            $("input:checkbox[name='_status']").bootstrapSwitch(switchOption);
            if(back_login == "0"){
                switchOption.state = false;
            }else{
                switchOption.state = true;
            }
            $("input:checkbox[name='_back_login']").bootstrapSwitch(switchOption);
            if(app_login == "0"){
                switchOption.state = false;
            }else{
                switchOption.state = true;
            }
            $("input:checkbox[name='_app_login']").bootstrapSwitch(switchOption);
        })

        

        $("#roleSave").click(function () {
            var validator = $("#editRoleForm").data("bs.validator");
            validator.validate();
            if (!validator.hasErrors()){
                $.ajax({
                    url: "{:U('Admin/Role/doEditRole')}",
                    type: "post",
                    dataType: "json",
                    data:$("#editRoleForm").serialize(),
                    success: function (data) {
                        if (data.status) {
                            $("#commonModal").modal('toggle');
                            common.alert("提示","用户组修改成功!",function(){
                                common.reload();
                            });
                        } else {
                            common.alert("提示",data.msg);
                        }
                    }
                });
            }
            
        })
    </script>